<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>文档</title>
  <link href="./css/semantic/semantic.min.css" rel="stylesheet">
  <link href="./css/markdown.css" rel="stylesheet">

    <link href="./css/wtxynavigation.css" rel="stylesheet">
    <script src="./js/jquery.min.js"></script>
    <script type="text/javascript" src="./js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>2.以UIView任意点旋转怎么做</div><div class='content'><h1>
<a id="布局" class="anchor" href="#%E5%B8%83%E5%B1%80"></a>布局</h1>
<ul>
<li>
<p>视图（<code>UIView</code>）有三种比较重要的布局属性：<code>frame</code>，<code>bounds</code>，<code>center</code></p>
</li>
<li>
<p>图层（<code>CALayer</code>）对应叫做：<code>frame</code>，<code>bounds</code>，<code>position</code>。</p>
</li>
<li>
<p><code>frame</code>并不是一个非常清晰的属性，它其实是一个虚拟属性，是根据<code>bounds</code>，<code>position</code>和<code>transform</code>计算而来</p>
<ul>
<li>所以当其中任何一个值发生改变，frame都会变化。</li>
<li>相反，改变frame的值同样会影响到他们当中的值</li>
</ul>
</li>
</ul>
<h1>
<a id="锚点anchorpoint" class="anchor" href="#%E9%94%9A%E7%82%B9anchorpoint"></a>锚点anchorPoint</h1>
<p>之前提到过，视图的<code>center</code>属性和图层的<code>position</code>属性都指定了<code>anchorPoint</code>相对于父图层的位置。图层的<code>anchorPoint</code>通过<code>position</code>来控制它的<code>frame</code>的位置，你可以认为<code>anchorPoint</code>是用来移动图层的_把柄_。</p>
<p>默认来说，<code>anchorPoint</code>位于图层的中点，所以图层的将会以这个点为中心放置。<code>anchorPoint</code>属性并没有被<code>UIView</code>接口暴露出来，这也是视图的position属性被叫做“center”的原因。但是图层的<code>anchorPoint</code>可以被移动，比如你可以把它置于图层<code>frame</code>的左上角，于是图层的内容将会向右下角的<code>position</code>方向移动（下图），而不是居中了。</p>
<p><img src="images/3.3.jpeg" alt="3.3"></p>
<ul>
<li>
<p><code>anchorPoint</code>用_单位坐标_来描述，也就是图层的相对坐标，图层左上角是{0, 0}，右下角是{1, 1}，因此默认坐标是{0.5, 0.5}。<code>anchorPoint</code>可以通过指定x和y值小于0或者大于1，使它放置在图层范围之外。</p>
</li>
<li>
<p>当改变了<code>anchorPoint</code>，<code>position</code>属性保持固定的值并没有发生改变，但是<code>frame</code>却移动了。</p>
</li>
</ul>
<pre lang="objc" class="objc"><code>
- (void)viewDidLoad

{

 [super  viewDidLoad];

  self.secondHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);

 self.minuteHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);

 self.hourHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);

  //start timer

  self.timer = [NSTimer  scheduledTimerWithTimeInterval:1.0  target:self  selector:@selector(tick) userInfo:nil  repeats:YES];

  //set initial hand positions

 [self tick];

}

- (void)tick

{

  //convert time to hours, minutes and seconds

  NSCalendar *calendar = [[NSCalendar  alloc] initWithCalendarIdentifier:NSGregorianCalendar];

  NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

 NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];

 CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;

  //calculate hour hand angle //calculate minute hand angle

 CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0;

  //calculate second hand angle

 CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0;

  //rotate hands

  self.hourHand.transform = CGAffineTransformMakeRotation(hoursAngle);

  self.minuteHand.transform = CGAffineTransformMakeRotation(minsAngle);

  self.secondHand.transform = CGAffineTransformMakeRotation(secsAngle);

}

</code></pre>
<h1>
<a id="答" class="anchor" href="#%E7%AD%94"></a>答:</h1>
<p>如下图, 如果是你想绕着一个图形的左上角旋转图片, 就设置如下代码</p>
<pre lang="c" class="c"><code>
// 设置
self.viewTest.layer.anchorPoint = CGPointMake(0, 0);



// 然后旋转
 static int second = 1;

 CGFloat secsAngle = (second / 60.0) * M_PI * 2.0;

 self.viewTest.transform = CGAffineTransformMakeRotation(secsAngle);

 second += 10;

</code></pre>
<p><img src="images/3.3.jpeg" alt="3.3"></p>
<h1>
<a id="扩展" class="anchor" href="#%E6%89%A9%E5%B1%95"></a>扩展</h1>
<p><a href="https://zsisme.gitbooks.io/ios-/content/chapter1/working-with-layers.html">iOS核心动画高级技巧</a></p>
<p><a href="https://zhangbuhuai.com/post/layer-geometry-in-ios.html">iOS 图层几何学</a></p>
<p><a href="https://gitee.com/wutongxd/QuestionsAndAnswersPro/tree/master/%E4%BB%BB%E6%84%8F%E7%82%B9%E6%97%8B%E8%BD%AC/UIViewTransformPoint">本节代码</a></p></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>